<?php
/**
 * config
 * 配置信息
 * @author Administrator
 * @version
 */
class Model_Tables_Config extends Zend_Db_Table_Abstract {
    /**
     * The default table name
     */
    protected $_name = 'config';
    
    public function add( $params )
    {
        $data = array(
		'configvalue' => $params['configvalue'],
		'description' => $params['description'],
		'configgroup' => $params['configgroup'],
        );
        return $this->insert($data);
    }
    public function modify( $params, $configname )
    {
    	$db = $this->getAdapter();
        $data = array(
		'configvalue' => $params['configvalue'],
		//'description' => $params['description'],
		//'configgroup' => $params['configgroup'],
        );
        $where = $db->quoteInto("configname=?", $configname);
        return $this->update($data , $where );
    }
    public function modifyajax( $configname, $field, $value )
    {
    	$db = $this->getAdapter();
    	$data = array($field => $value);
        $where = $db->quoteInto("configname=?", $configname);
        return $this->update($data , $where );
    }
    public function modifyAjaxRevers( $field ,$pri)
    {
    	$db = $this->getAdapter();
    	$sql = "update config set  $field =not  $field  where configname=' $pri'";
		return $db->query($sql);
    }
    public function del( $configname )
    {
     	$where = 'configname = '.$configname;
        return $this->delete($where);
    }
    public function getInfo($configname)
    {
        $row = $this->fetchRow("configname = '$configname'");
        if (!$row) {
        	return null;
        }
        else
        {
        	return $row->toArray();
        }
    }
    public function getCount($s = "")
    { 
        $select = $this->select()->from($this->_name,'count(*) as num');
    	if($s!="")  
    	{
    		$db = $this->getAdapter();
			$where = $db->quoteInto("configgroupid=?", $s);
    		$select = $select->where($where);
    	}
        $result = $this->fetchRow($select)->num;
        return $result;
    }
    public function getList($page, $numPerPage = 10,$s= "")
    {
    	$page = (1>$page) ? 1 : $page;
    	$offset = ($page-1)*$numPerPage;
        $orderby = array('configname');
        if($s!="")     
		{
			$db = $this->getAdapter();
			$where = $db->quoteInto("configgroupid= ?", $s);
		}  
        $result = $this->fetchAll($where,$orderby, $numPerPage, $offset )->toArray();
        return $result;
    }
    
 	public function getGroups()
    {
        $select = $this->select()->from($this->_name, array('configGroup','configgroupid'))
        ->order("configgroupid")
        ->where("configgroupid>0");
        $result = $this->fetchAll($select);
    	$roles = array();
        if (!empty($result))
	        {
	            foreach ($result as $item)
	            {
	                $roles[$item['configgroupid']] = $item['configGroup'];
	            }
	        }
        return $roles;
    }
}